package com.cn.demo.service.impl;

import com.cn.demo.entity.Curriculum;
import com.cn.demo.entity.Evaluate;
import com.cn.demo.mapper.CurriculumMapper;
import com.cn.demo.mapper.EvaluateMapper;
import com.cn.demo.mapper.TeacherMapper;
import com.cn.demo.service.CurriculumService;
import com.cn.demo.service.EvaluateService;
import com.cn.demo.service.PageInfoUtilServer;
import com.cn.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class CurriculumServiceImpl implements CurriculumService {

    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private PageInfoUtilServer pageInfoUtilServer;

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Override
    public Result add(Curriculum curriculum) {
        Date dateNow = new Date();
        if (dateNow.after(curriculum.getStartDate())){
            return Result.error("起始日期必须是当前时间后");
        }
        if (curriculum.getStartDate().after(curriculum.getEndDate())){
            return Result.error("结束日期必须是开始时间后");
        }
        //添加课程需要添加老师
        if (teacherMapper.getByTeacherId(curriculum.getTeacherId()) == null) {
            return Result.error("教师不存在");
        }
        curriculum.setCurriculumId(UUID.randomUUID().toString());
        List<String> project = curriculum.getProject();
        if (project.size() == 0) {
            return Result.error("至少选择一个项目");
        }
        for (String s : project) {
            if ("设计".equals(s)) {
                curriculum.setCurriculumDesignId(UUID.randomUUID().toString());
            }
            if ("实验".equals(s)) {
                curriculum.setCurriculumExperimentId(UUID.randomUUID().toString());
            }
            if ("实习".equals(s)) {
                curriculum.setCurriculumInternshipId(UUID.randomUUID().toString());
            }
        }
        if (curriculumMapper.add(curriculum) > 0) {
            return Result.success();
        }
        return Result.error();
    }


    @Override
    public Result get(Integer page, Integer limit, Curriculum curriculum) {
        PageHelper.startPage(page, limit);
        List<Curriculum> curriculumList = curriculumMapper.get(curriculum);
        return Result.success(pageInfoUtilServer.currency(curriculumList));
    }

    @Override
    public Result getByCurriculumId(String curriculumId) {
        Curriculum curriculum = curriculumMapper.getByCurriculumId(curriculumId);
        List<String> nowProject = new ArrayList<>();
        if (curriculum.getCurriculumDesignId() != null) {
            nowProject.add("设计");
        }

        if (curriculum.getCurriculumExperimentId() != null) {
            nowProject.add("实验");
        }

        if (curriculum.getCurriculumInternshipId() != null) {
            nowProject.add("实习");
        }
        curriculum.setProject(nowProject);
        if (curriculum != null) {
            return Result.success(curriculum);
        }
        return Result.error();
    }

    @Override
    public Result update(Curriculum curriculum) {
        if (curriculumMapper.updateByPrimaryKey(curriculum) > 0) {
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result getByStudentId(Integer page, Integer limit, Curriculum curriculum, String studentId) {
        PageHelper.startPage(page, limit);
        List<Curriculum> curriculumList = curriculumMapper.getByStudentId(curriculum, studentId);
        for (Curriculum item : curriculumList) {
            if (item.getCurriculumDesignId()!=null){
                Evaluate CurriculumDesign = evaluateMapper.getEvaluate(studentId, item.getCurriculumDesignId());
                if (CurriculumDesign!=null){
                    item.setCurriculumDesignEvaluate(CurriculumDesign.getRank());
                }else{
                    item.setCurriculumDesignEvaluate("未打分");
                }
            }else {
                item.setCurriculumDesignEvaluate("无");
            }

            if (item.getCurriculumExperimentId()!=null){
                Evaluate CurriculumExperiment = evaluateMapper.getEvaluate(studentId, item.getCurriculumExperimentId());
                if (CurriculumExperiment!=null){
                    item.setCurriculumExperimentEvaluate(CurriculumExperiment.getRank());
                }else{
                    item.setCurriculumExperimentEvaluate("未打分");
                }
            }else {
                item.setCurriculumExperimentEvaluate("无");
            }

            if (item.getCurriculumInternshipId()!=null){
                Evaluate CurriculumInternship = evaluateMapper.getEvaluate(studentId, item.getCurriculumInternshipId());
                if (CurriculumInternship!=null){
                    item.setCurriculumInternshipEvaluate(CurriculumInternship.getRank());
                }else{
                    item.setCurriculumInternshipEvaluate("未打分");
                }
            }else {
                item.setCurriculumInternshipEvaluate("无");
            }
        }
        return Result.success(pageInfoUtilServer.currency(curriculumList));


    }

    @Override
    public Result getCurriculumByTeacherId(Integer page, Integer limit, String teacherId) {
        if (teacherMapper.getByTeacherId(teacherId) == null) {
            return Result.error("教师不存在");
        }
        PageHelper.startPage(page, limit);
        List<Curriculum> curriculumList = curriculumMapper.getCurriculumByTeacherId(teacherId);
        return Result.success(pageInfoUtilServer.currency(curriculumList));
    }
}